// 1. 定义路由组件.
// 也可以从其他文件导入

// 2. 定义一些路由
// 每个路由都需要映射到一个组件。
// 我们后面再讨论嵌套路由。
import {createRouter, createWebHashHistory, createWebHistory} from "vue-router";

const routes = [
    {path: '/home', component: () => import('../../views/chap13/Home.vue')},
    {path: '/about', component: () => import('../../views/chap13/About.vue')},
    // 动态字段以冒号开始
    {path: '/order/:id', component: () => import('../../views/chap13/Order.vue')},
    // {path: '/product/:id(\\d+)', component: () => import('../../views/chap13/Product.vue')},
    // {path: '/product/:id(.*)', component: () => import('../../views/chap13/Product.vue')},
    {path: '/product/:id(.?)', component: () => import('../../views/chap13/Product.vue')},
    {
        path: '/lhzA',
        component: () => import('../../views/chap13/LhzA.vue'),
        children: [
            {
                path: 'lhzB',
                component: () => import('../../views/chap13/LhzB.vue'),
            },
            {
                path: 'lhzC',
                component: () => import('../../views/chap13/LhzC.vue'),
            }
        ]
    },
    {
        name: 'nameA',
        alias: ['/tta', '/ta'],
        path: '/testA',
        component: () => import('../../views/chap13/TestA.vue')
    },
    {
        name: 'nameB',
        path: '/testB/:id',
        component: () => import('../../views/chap13/TestB.vue')
    },
    {path: '/testC', component: () => import('../../views/chap13/TestC.vue')},
    {
        name: 'testDid',
        path: '/testD/:id',
        component: () => import('../../views/chap13/TestD.vue')
    },
    {
        name: 'nameE',
        path: '/testE/:id',
        component: () => import('../../views/chap13/TestE.vue'),
        props: true
    },
    {
        name: 'nameF',
        path: '/testF',
        component: () => import('../../views/chap13/TestF.vue'),
        props: route => ({ id: route.query.id })
    },
    {
        path: '/',
        components: {
            default: () => import('../../views/chap13/MainContent.vue'),
            // LeftSidebar: LeftSidebar 的缩写
            LeftSidebar: () => import('../../views/chap13/LeftSidebar.vue'),
            // LeftSidebar: LeftSidebar 的缩写,
            // 它们与 `<router-view>` 上的 `name` 属性匹配
            RightSidebar: () => import('../../views/chap13/RightSidebar.vue'),
            // LeftSidebar: LeftSidebar 的缩写,
        },
    },
    {
        path: '/test',
        // redirect: {name: 'nameA'}
        // redirect: '/testA'
        redirect: '/tta'
    },
    {
        // /search/screens -> /search?q=screens
        path: '/search/:id',
        redirect: to => {
            // 方法接收目标路由作为参数
            // return 重定向的字符串路径/路径对象
            // return {path: '/testC', query: {id: to.params.id}}
            // return {path: '/testC', query: {id: to.params.id}}
            return {name: 'nameB', params: {id: to.params.id}}
            // console.log(to.query.id)
        },
    },
    {path: '/:pathMatch(.*)*', component: () => import('../../views/chap13/NotFound.vue')},
]

// 3. 创建路由实例并传递 `routes` 配置
// 你可以在这里输入更多的配置，但我们在这里
// 暂时保持简单
const router = createRouter({
    // 4. 内部提供了 history 模式的实现。为了简单起见，我们在这里使用 hash 模式。
    // history: createWebHashHistory(),
    history: createWebHistory(),
    routes, // `routes: routes` 的缩写
})
export default router